package com.freshcity.page.tree
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.FilterConst;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.tree.InWaterWin;
	import com.freshcity.tree.WaterItem;
	import com.freshcity.utils.GlobalUtil;
	import com.freshcity.utils.MessageUtil;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class InWater extends Component
	{
		private var back:InWaterWin;
		private var waterBar:WaterBar;
		private var data:*;
		private var selectedWater:WaterItem;
		
		public function InWater(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			back.caijiBtn.addEventListener(MouseEvent.CLICK,onCaijiBtnClick);
			back.removeBtn.addEventListener(MouseEvent.CLICK,onRemoveBtnClick);
			this.waterBar.addEventListener(Event.SELECT,onWaterSelectHandler);
		}
		
		protected function onWaterSelectHandler(event:DataEvent):void
		{
			this.selectedWater = event.data as WaterItem;
			back.removeBtn.enabled = true;
			back.removeBtn.visible = true;
		}
		
		protected function onRemoveBtnClick(event:MouseEvent):void
		{
			back.removeBtn.enabled = false;
			main.commu.send(OpCodes.REMOVE_WATER,{index:selectedWater.index},removeWaterCallback);
		}
		
		private function removeWaterCallback(data:*):void
		{
			main.msg.showFixRollMsg("丢掉了 "+ MessageUtil.decodeWater(selectedWater.category));
			back.removeBtn.visible = false;
			main.user.waterInfo = data;
			waterBar.setData(main.user.waterInfo);
		}
		
		protected function onCaijiBtnClick(event:MouseEvent):void
		{
			if(main.user.energy<GlobalVar.GATHER_WATER_ENERGY){
				main.msg.showRollTipsMsg("今日体力已经不足");
				return;
			}
			back.caijiBtn.labelName.text="工作中...";
			switchBtnStatus(false);
			
			main.statusPanel.timeBar.data=48;
			main.statusPanel.timeBar.callBackFun = onFinishTimeCallback;
		}
		
		private function onFinishTimeCallback():void
		{
			var placeId:int = int(back.shichang.value);
			main.commu.send(OpCodes.FINISH_IN_WATER,{pid:placeId},finishCallBack);
		}
		
		private function finishCallBack(data:*):void
		{
			if(data == -2){
				main.msg.showRollTipsMsg("今日体力已不足");
			}else{
				renderInWaterResult(data);
				main.commu.send(OpCodes.INIT_IN_WATER,{});
			}
		}
		
		public function renderInWaterResult(index:int):void
		{
			var msg:String="";
			switch (index)
			{
				case 0:
				{
					msg="清风徐来,你突然感觉浑身充满了力量";
					main.msg.showPopUpMsg(msg,MsgManager.COOL,3,MsgManager.CENTER);
					break;
				}
				case -1:
				{
					msg="经过一阵辛苦工作,却一无所获";
					main.msg.showPopUpMsg(msg,MsgManager.CRY,3,MsgManager.CENTER);
					break;
				}
				case -5:
				{
					msg="不幸遇到意外,你受伤了";
					main.msg.showPopUpMsg(msg,MsgManager.CRY,3,MsgManager.CENTER);
					break;
				}
					
				default:
				{
					msg="成功采集到：" + MessageUtil.decodeWater(index);
					main.msg.showPopUpMsg(msg,MsgManager.HAHA,3,MsgManager.CENTER);
					break;
				}
			}
			main.msg.showFixRollMsg(msg);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new InWaterWin();
			addChild(back);
			back.removeBtn.visible=false;
			back.caijiBtn.buttonMode=true;
			waterBar = new WaterBar(back,130,246);
		}
		
		public function setWaterData(data:*):void
		{
			EffectManager.hideLoadMask();
			this.data = data;
			initWater();
		}
		
		override public function show(event:Event=null):void
		{
			Dispatcher.addEventListener(EventName.FinishTimeTick,initWater);
			EffectManager.addLoadMask(back);
			waterBar.main = main;
			main.commu.send(OpCodes.INIT_IN_WATER,{},setWaterData);
			super.show(event);
		}
		
		private function switchBtnStatus(flag:Boolean):void
		{
			back.caijiBtn.mouseEnabled=flag;
			back.caijiBtn.mouseChildren=flag;
			back.caijiBtn.labelName.mouseEnabled=false;
		}		
		
		private function initWater(event:Event=null):void
		{
			back.tianqi.text = MessageUtil.decodeWeather(main.user.weather);
			back.shichen.text = MessageUtil.decodePeriod(data.period);
			main.user.waterInfo = data.waterInfo;
			waterBar.setData(main.user.waterInfo);
			
			back.caijiBtn.filters=[];
			if (main.statusPanel.timeBar.isTick)
			{
				back.caijiBtn.labelName.text="工作中...";
				switchBtnStatus(false);
			}else if (main.user.waterInfo.indexOf("0")<=-1)
			{
				back.caijiBtn.labelName.text="蓄水充足";
				switchBtnStatus(false)
			}else{
				back.caijiBtn.labelName.text="开始采集";
				switchBtnStatus(true);
			}
			
			if(main.user.energy<GlobalVar.GATHER_WATER_ENERGY){
				back.caijiBtn.filters=[FilterConst.colorMatrixFilterGray];
			}
			
			if(!GlobalUtil.guideIsDone(3) && main.user.sex=='male'){
				TweenLite.delayedCall(1,onShowGuide);
			}
		}
		
		private function forwardPage():void
		{
			main.guideTip.hideGuideTips();
			main.commu.send(OpCodes.GUIDE_DONE, {indexPos:3});
		}
		
		private function onShowGuide():void
		{
			var s:String = "许多梧桐树正需要浇灌，而她们的女主人正期待你能赠些清水。赶快行动！你的真情会为你赢得芳心！";
			main.guideTip.getGuideTips(s,forwardPage,0,null,false);
		}		
		
		override public function destroy():void
		{
			Dispatcher.removeEventListener(EventName.FinishTimeTick,initWater);
			back.removeBtn.removeEventListener(MouseEvent.CLICK,onRemoveBtnClick);
			this.waterBar.removeEventListener(Event.SELECT,onWaterSelectHandler);
			back.caijiBtn.removeEventListener(MouseEvent.CLICK,onCaijiBtnClick);
			super.destroy();
		}
		
	}
}